package cn.com.gome.generator.logic;

import cn.com.gome.framework.dao.entity.TblChildProjectInfo;
import cn.com.gome.framework.dao.entity.TblProjectBasicInfo;
import com.gomeplus.frame.exception.LogicsException;
import com.gomeplus.frame.logic.ILogics;
import com.gomeplus.frame.logic.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

/**
 * @author chenmin-ds
 * @ClassName: MainProjectCreateLogic
 * @Description: 主项目创建
 * @date 2017年5月26日 下午2:49:05
 */
@Service
public class ChildProjectDaoCreateLogic extends AbstractLogic implements ILogics<Map<String, Object>> {

    private Logger logger = LoggerFactory.getLogger("ChildProjectDaoCreateLogic");

    @Override
    public ResultEnum exec(Map<String, Object> map) throws LogicsException {
        try {
            TblChildProjectInfo childProjectInfo = (TblChildProjectInfo) map.get("childProjectInfo");
            TblProjectBasicInfo projectBasicInfo = (TblProjectBasicInfo) map.get("projectBasicInfo");
            String pachagesPath = projectBasicInfo.getPackages().replace(".", "//");

            String projectPath = projectBasicInfo.getProjectPath().trim();
            if (!projectPath.endsWith("//")) {
                projectPath += "//";
                projectBasicInfo.setProjectPath(projectPath);
            }

            projectPath += projectBasicInfo.getProjectEng() + "//";
            projectPath += projectBasicInfo.getProjectEng() + "-" + childProjectInfo.getChildProjectEng();
            childProjectInfo.setProjectPath(projectPath);

            // 创建存储vo的文件
            File file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//vo");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//" + childProjectInfo.getChildProjectEng() + "//entity");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//" + childProjectInfo.getChildProjectEng() + "//mapper//ser");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//java//" + pachagesPath + "//" + projectBasicInfo.getProjectEng() + "//" + childProjectInfo.getChildProjectEng() + "//mapper//edit");
            if (!file.exists()) {
                file.mkdirs();
            }
            // 创建JDBC文档
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc");
            if (!file.exists()) {
                file.mkdirs();
            }
            // 创建dev环境
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//dev");
            if (!file.exists()) {
                file.mkdirs();
            }
            StringBuffer sb = new StringBuffer();
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.checkoutTimeout=5000\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.minPoolSize=10\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.maxPoolSize=50\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.maxIdleTime=7200\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.acquireIncrement=3\n");
            sb.append(projectBasicInfo.getProjectEng() + ".cpool.maxIdleTimeExcessConnections=3000\n");
            sb.append("\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.driverClassName=" + projectBasicInfo.getDatabaseDriver() + "\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.url=" + projectBasicInfo.getDatabaseUrl() + "\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.username=" + projectBasicInfo.getDatabaseAccount() + "\n");
            sb.append(projectBasicInfo.getProjectEng() + ".jdbc.password=" + projectBasicInfo.getDatabasePassword() + "\n");
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//dev//" + projectBasicInfo.getProjectEng() + "-jdbc.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//dev//" + projectBasicInfo.getProjectEng() + "-jdbc.properties"), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//uat");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//uat//" + projectBasicInfo.getProjectEng() + "-jdbc.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//uat//" + projectBasicInfo.getProjectEng() + "-jdbc.properties"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//live");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//live//" + projectBasicInfo.getProjectEng() + "-jdbc.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//live//" + projectBasicInfo.getProjectEng() + "-jdbc.properties"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//pre");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//pre//" + projectBasicInfo.getProjectEng() + "-jdbc.properties");
            if (!file.exists()) {
                file.createNewFile();
            }
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//jdbc//pre//" + projectBasicInfo.getProjectEng() + "-jdbc.properties"), "UTF-8");
            writer = new BufferedWriter(write);
            writer.write(sb.toString());
            writer.flush();
            writer.close();
            // XML 文档
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//mapper//ser");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//props//" + projectBasicInfo.getProjectEng() + "//mapper//edit");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//spring");
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(projectPath + "//src//main//resources//spring//" + projectBasicInfo.getProjectEng() + "-dataSource.xml");
            if (!file.exists()) {
                file.createNewFile();
            }

            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//spring//" + projectBasicInfo.getProjectEng() + "-dataSource.xml"), "UTF-8");
            StringBuffer sbDataSource = new StringBuffer();
            sbDataSource.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            sbDataSource.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"\n");
            sbDataSource.append("	xmlns:aop=\"http://www.springframework.org/schema/aop\" xmlns:context=\"http://www.springframework.org/schema/context\"\n");
            sbDataSource.append("	xmlns:tx=\"http://www.springframework.org/schema/tx\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
            sbDataSource.append("	xmlns:mongo=\"http://www.springframework.org/schema/data/mongo\"\n");
            sbDataSource.append("	xsi:schemaLocation=\"http://www.springframework.org/schema/aop\n");
            sbDataSource.append("        http://www.springframework.org/schema/aop/spring-aop.xsd\n");
            sbDataSource.append("        http://www.springframework.org/schema/beans\n");
            sbDataSource.append("        http://www.springframework.org/schema/beans/spring-beans.xsd\n");
            sbDataSource.append("        http://www.springframework.org/schema/context \n");
            sbDataSource.append("        http://www.springframework.org/schema/context/spring-context.xsd\n");
            sbDataSource.append("        http://www.springframework.org/schema/tx  \n");
            sbDataSource.append("        http://www.springframework.org/schema/tx/spring-tx.xsd\">\n");
            sbDataSource.append("\n");
            sbDataSource.append("	<bean id=\"" + projectBasicInfo.getProjectEng() + "DataSource\" class=\"org.apache.commons.dbcp.BasicDataSource\">\n");
            sbDataSource.append("		<property name=\"driverClassName\">\n");
            sbDataSource.append("			<value>${" + projectBasicInfo.getProjectEng() + ".jdbc.driverClassName}</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"url\">\n");
            sbDataSource.append("			<value>${" + projectBasicInfo.getProjectEng() + ".jdbc.url}</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"username\">\n");
            sbDataSource.append("			<value>${" + projectBasicInfo.getProjectEng() + ".jdbc.username}</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"password\">\n");
            sbDataSource.append("			<value>${" + projectBasicInfo.getProjectEng() + ".jdbc.password}</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"initialSize\">\n");
            sbDataSource.append("			<value>${" + projectBasicInfo.getProjectEng() + ".cpool.minPoolSize}</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"maxActive\">\n");
            sbDataSource.append("			<value>${" + projectBasicInfo.getProjectEng() + ".cpool.maxPoolSize}</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"maxIdle\">\n");
            sbDataSource.append("			<value>15</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"minIdle\">\n");
            sbDataSource.append("			<value>5</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"maxWait\">\n");
            sbDataSource.append("			<value>60000</value>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("		<property name=\"testOnBorrow\" value=\"true\"></property>\n");
            sbDataSource.append("		<property name=\"testOnReturn\" value=\"true\"></property>\n");
            sbDataSource.append("		<property name=\"testWhileIdle\" value=\"true\"></property>\n");
            sbDataSource.append("		<property name=\"timeBetweenEvictionRunsMillis\" value=\"36000\"></property>\n");
            sbDataSource.append("	</bean>\n");
            sbDataSource.append("\n");
            sbDataSource.append("	<bean id=\"" + projectBasicInfo.getProjectEng() + "SqlSessionFactory\" class=\"org.mybatis.spring.SqlSessionFactoryBean\">\n");
            sbDataSource.append("		<property name=\"dataSource\" ref=\"" + projectBasicInfo.getProjectEng() + "DataSource\" />\n");
            sbDataSource.append("		<property name=\"mapperLocations\">\n");
            sbDataSource.append("			<list>\n");
            sbDataSource.append("				<value>classpath*:/props/" + projectBasicInfo.getProjectEng() + "/mapper/*/*.xml</value>\n");
            sbDataSource.append("			</list>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("	</bean>\n");
            sbDataSource.append("	<bean class=\"org.mybatis.spring.mapper.MapperScannerConfigurer\">\n");
            sbDataSource.append("		<property name=\"basePackage\" value=\"" + projectBasicInfo.getPackages() + "." + projectBasicInfo.getProjectEng() + ".dao.mapper\" />\n");
            sbDataSource.append("		<property name=\"sqlSessionFactoryBeanName\" value=\"" + projectBasicInfo.getProjectEng() + "SqlSessionFactory\" />\n");
            sbDataSource.append("		<property name=\"annotationClass\" value=\"com.gomeplus.jdbc.mybatis.MybatisRepository\" />\n");
            sbDataSource.append("	</bean>\n");
            sbDataSource.append("	<bean id=\"txManager" + lowerToUpper(projectBasicInfo.getProjectEng()) + "\"	class=\"org.springframework.jdbc.datasource.DataSourceTransactionManager\">\n");
            sbDataSource.append("		<property name=\"dataSource\" ref=\"" + projectBasicInfo.getProjectEng() + "DataSource\"></property>\n");
            sbDataSource.append("	</bean>\n");
            sbDataSource.append("	<tx:annotation-driven transaction-manager=\"txManager" + lowerToUpper(projectBasicInfo.getProjectEng()) + "\" proxy-target-class=\"true\" />\n");
            sbDataSource.append("\n");
            sbDataSource.append("</beans>\n");
            writer = new BufferedWriter(write);
            writer.write(sbDataSource.toString());
            writer.flush();
            writer.close();

            file = new File(projectPath + "//src//main//resources//spring//" + projectBasicInfo.getProjectEng() + "-jdbc-props.xml");
            if (!file.exists()) {
                file.createNewFile();
            }
            write = new OutputStreamWriter(new FileOutputStream(projectPath + "//src//main//resources//spring//" + projectBasicInfo.getProjectEng() + "-jdbc-props.xml"), "UTF-8");
            sbDataSource = new StringBuffer();
            sbDataSource.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            sbDataSource.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"\n");
            sbDataSource.append("	xmlns:context=\"http://www.springframework.org/schema/context\"\n");
            sbDataSource.append("	xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
            sbDataSource.append("	xmlns:mongo=\"http://www.springframework.org/schema/data/mongo\"\n");
            sbDataSource.append("	xsi:schemaLocation=\"http://www.springframework.org/schema/beans  \n");
            sbDataSource.append("        http://www.springframework.org/schema/beans/spring-beans.xsd \n");
            sbDataSource.append("        http://www.springframework.org/schema/context              \n");
            sbDataSource.append("        http://www.springframework.org/schema/context/spring-context.xsd\">\n");
            sbDataSource.append("	\n");
            sbDataSource.append("	<bean class=\"com.gomeplus.security.aes.PropertyEncryptConfig\">\n");
            sbDataSource.append("		<property name=\"locations\">\n");
            sbDataSource.append("			<list>\n");
            sbDataSource.append("				<value>classpath*:/props/" + projectBasicInfo.getProjectEng() + "/jdbc/${env}/" + projectBasicInfo.getProjectEng() + "-jdbc.properties</value>\n");
            sbDataSource.append("			</list>\n");
            sbDataSource.append("		</property>\n");
            sbDataSource.append("	</bean>\n");
            sbDataSource.append("	\n");
            sbDataSource.append("</beans>\n");

            writer = new BufferedWriter(write);
            writer.write(sbDataSource.toString());
            writer.flush();
            writer.close();

            return ResultEnum.OK;
        } catch (Exception e) {
            logger.error("创建子项目异常：", e);
        }
        return ResultEnum.PART_CASE_01;
    }

}
